class PostController < ApplicationController

            def page1
                @categories = Category.find(:all, :include => [:sub_categories])
                @all_categories = Category.categories_for_student(true)
            

            end

            def page2
              @verification_code=Random.alphanumeric(6)
              @user=User.create(:full_name=>params[:user][:name],:email=>params[:user][:email],:mobile_no=>params[:user][:mobile_no],:verification_code=>@verification_code)
              @user.save
              @lastuser=User.find(:last)
              @university   = University.find_by_stripped_name(params[:university])
              @sub_category = SubCategory.find_by_stripped_name(params[:sub_category][:sub_cat_name])
              require 'cgi'

#sending sms and email
#                    sendsms(params[:user][:mobile_no],CGI.escape('Verification Code: '+@verification_code))
#                     if params[:user][:email]
#                     recipient = params[:user][:email]
#                     message='Verification Code: '+@verification_code
#                     subject = 'Verify Your Post'
#                     Mailer.deliver_contact(recipient, subject, message)
#                     return if request.xhr?
#                     render :text => 'Message sent successfully'
#                     end

                     
##########################################################################################################
            end

            def page3

             logger.info "------------------------------------------------------"
             logger.info params.inspect
             logger.info "------------------------------------------------------"

               if request.post?
                       @advertisment = Advertisment.new(params[:advertisment])
                       @advertisment.advertisment_attribute_field_value = AdvertismentAttributeFieldValue.create(:field_values => params[:advertisment_attributes])
                       @advertisment.sub_category = SubCategory.find_by_id(params[:sub_category])
                       @advertisment.university = University.find_by_id(params[:university])
                       @advertisment.user = User.find_by_id(params[:user])
                       @advertisment.save
                       session[:advertisment_id] = @advertisment.id


                  if (!params[:upload][:file1].nil?)
                      attachment = Attachment.create
                      attachment.file = params[:upload][:file1]
                      attachment.advertisment_id = @advertisment.id
                      attachment.save
                  end
                  if (!params[:upload][:file2].nil?)
                      attachment = Attachment.create
                      attachment.file = params[:upload][:file2]
                      attachment.advertisment_id = @advertisment.id
                      attachment.save
                  end
                  if (!params[:upload][:file3].nil?)
                      attachment = Attachment.create
                      attachment.file = params[:upload][:file3]
                      attachment.advertisment_id = @advertisment.id
                      attachment.save
                  end
                  if (!params[:upload][:file4].nil?)
                      attachment = Attachment.create
                      attachment.file = params[:upload][:file4]
                      attachment.advertisment_id = @advertisment.id
                      attachment.save
                  end
               #sendsms('9953805531','Hello')
               end
               if params[:update]=='1'
                       @advertisment = Advertisment.find_by_id(session[:advertisment_id])
                       @advertisment.sub_category = SubCategory.find_by_id(params[:sub_category])
                       @advertisment.university = University.find_by_id(params[:university])
                       @advertisment.user = User.find_by_id(params[:user])
               end
             end
             def edit_full_name
                render :layout => false
             end
             def update_full_name
                User.update(params[:id], :full_name => params[:fullname_text])
                @user=User.find_by_id(params[:id])
                render :layout => false

             end
             def edit_email
                render :layout => false
             end
             def update_email
                User.update(params[:id], :email => params[:email_text])
                @user=User.find_by_id(params[:id])
                render :layout => false

             end
             def edit_mobile
                render :layout => false
             end
             def update_mobile
                User.update(params[:id], :mobile_no => params[:mobile_text])
                @user=User.find_by_id(params[:id])
                render :layout => false
             end
             def edit_university
                render :layout => false
             end
             def update_university
                @university=University.find_by_stripped_name(params[:university])
                Advertisment.update(params[:id], :university_id => @university.id)
                @advertisment=Advertisment.find_by_id(params[:id])
                render :layout => false
             end
             def update_status
                @user=User.find_by_id(params[:id])
                if @user.verification_code == params[:verification_code]
                 User.update(params[:id], :status =>  1)
                @message = "verified successfully"
                else
                @message = "invalid code"
                end
                render :layout => false
             end
             def edit_post
               @advertisment=Advertisment.find_by_id(session[:advertisment_id])
               @sub_category = SubCategory.find_by_id( @advertisment.sub_category_id)
               @advertisment_attribute = @advertisment.advertisment_attribute_field_value
               @field_values           = YAML.load(@advertisment_attribute.field_values)
               @images                 = @advertisment.attachments
             end
             def update_post
               if request.post?
               Advertisment.update(session[:advertisment_id],params[:advertisment])
               @advertisment=Advertisment.find_by_id(session[:advertisment_id])
               @advertisment.sub_category = SubCategory.find_by_id(params[:sub_category])
               @advertisment.university = University.find_by_id(params[:university])
               @advertisment.user = User.find_by_id(params[:user])
               @advertisment.advertisment_attribute_field_value = AdvertismentAttributeFieldValue.update(params[:field_id],:field_values => params[:advertisment_attributes])
#                  if (!params[:upload][:file1].nil?)
#                      attachment = Attachment.create
#                      attachment.file = params[:upload][:file1]
#                      attachment.advertisment_id = @advertisment.id
#                      attachment.save
#                  end
#                  if (!params[:upload][:file2].nil?)
#                      attachment = Attachment.create
#                      attachment.file = params[:upload][:file2]
#                      attachment.advertisment_id = @advertisment.id
#                      attachment.save
#                  end
#                  if (!params[:upload][:file3].nil?)
#                      attachment = Attachment.create
#                      attachment.file = params[:upload][:file3]
#                      attachment.advertisment_id = @advertisment.id
#                      attachment.save
#                  end
#                  if (!params[:upload][:file4].nil?)
#                      attachment = Attachment.create
#                      attachment.file = params[:upload][:file4]
#                      attachment.advertisment_id = @advertisment.id
#                      attachment.save
#                  end


               redirect_to :action=>"page3", :user=>params[:user],:sub_category=>params[:sub_category],:university=>params[:university],:update=>"1"
              
               end
             end


 end